package com.zelin.web.controller;

import com.github.pagehelper.PageInfo;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/15-16:25
 * ------------------------------
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    //1. 查询所有学生
    @Autowired
    private StudentService studentService;
    @GetMapping("/list")
    public R list(){
        return R.ok().data("list",studentService.findAll());
    }

    //2. 分页带条件查询
    //@RequestBody: 将后台的json串转换为java类
    //@ResponseBody：将后台的java对象转换为json串
    @PostMapping("/search")
    public R search(int page, int pageSize,@RequestBody(required = false) Student student){
        PageInfo<Student> search = studentService.search(page, pageSize, student);
        R data = R.ok().data("total", search.getTotal()).data("rows", search.getList());
        System.out.println("data = " + data);
        return data;
    }

    //3. 添加学生
    @PostMapping("/add")
    public R add(@RequestBody Student student){
        studentService.insert(student);
        return R.ok();
    }

    //4. 修改学生
    @PostMapping("/update")
    public R update(@RequestBody Student student){
        studentService.update(student);
        return R.ok();
    }

    //5. 删除学生
    @PostMapping("/delete")
    public R delete(String sid){
        studentService.delete(sid);
        return R.ok();
    }
}

